trigger UpdateRoutingPlan on Routing_Plan_And_Visit__c (after update,after delete)
{
    List<Id> listR=new List<Id>();
    List<Id> listO=new List<Id>();
    for(Routing_Plan_And_Visit__c rpvList:trigger.old)
    {
        listO.add(rpvList.Id);
    }
    
    if(trigger.isUpdate)
    {
        boolean hasUpdate=false;
        for(Integer i=0;i<trigger.new.size();i++){
            if(Trigger.new[i].Visit_Date__c!=Trigger.old[i].Visit_Date__c)
            {
                hasUpdate=true;
            }   
        }
        if(hasUpdate)
        {
            String filter='';
            for(integer i = 0; i < trigger.new.size(); i++)
            {
                //if((trigger.new[i].PlanNo__c != trigger.old[i].PlanNo__c || trigger.new[i].VisitNo__c != trigger.old[i].VisitNo__c)
                //  && trigger.new[i].Visit_Date__c == trigger.old[i].Visit_Date__c)
                //{
                //  continue;
                //}
                //else
                //{
                    if(filter!='')
                        filter+=' or ';
                    
                    filter+='RoutingPlanAndVisit__c=\''+trigger.new[i].Id+'\'';
                //}
            }
            if(filter!='')
            {
                filter = ' where ' + filter;
            }
            /*for(Routing_Plan_And_Visit__c rpvList:trigger.old)
            {
                if(filter!='')
                    filter+=' or ';
                    
                filter+='RoutingPlanAndVisit__c=\''+rpvList.Id+'\'';
            }*/
            String sql='select Id,OutletName__c,RoutingPlanAndVisit__c,StartDateTime__c from RoutingPlan__c'+filter;
            List<RoutingPlan__c> rp=DataBase.query(sql);
             //[select Id,OutletName__c,RoutingPlanAndVisit__c,StartDateTime__c from RoutingPlan__c where RoutingPlanAndVisit__c in:listR];
            List<RoutingPlan__c> updateRp=new List<RoutingPlan__c>();
            
            for(Integer i=0;i<Trigger.new.size();i++)//New
            {
                if(Trigger.New[i].Visit_Date__c != Trigger.Old[i].Visit_Date__c)
                {
                    Date dt=Trigger.new[i].Visit_Date__c;
                    Integer Minute=0;
                    DateTime visitDt;
                    
                    for(RoutingPlan__c re:rp)//Old
                    {
                        Date starD=re.StartDateTime__c.Date();
                        if(Trigger.new[i].Id==re.RoutingPlanAndVisit__c && starD != Trigger.new[i].Visit_Date__c)
                        {
                            visitDt=DateTime.newInstance(dt.year(),dt.month(),dt.day(),8,Minute,1);
                            re.StartDateTime__c=visitDt;
                            re.EndDateTime__c=visitDt.addMinutes(30);
                            re.PlanStatus__c='waiting';
                            Minute+=30;
                            updateRp.add(re);
                        }
                    }
                }
            }
             try
             { 
                if(updateRp.size()>0)
                    update updateRp;
             }
             catch(DmlException ex)
             {
                Trigger.new[0].addError('不能修改当前的记录');
                //rpv.addError('不能修改当前的记录');
             }
            
            /*for(Routing_Plan_And_Visit__c rpv:trigger.new)
            {
                List<RoutingPlan__c> rp=[select Id,OutletName__c from RoutingPlan__c where RoutingPlanAndVisit__c=:rpv.Id];
                 Date dt=rpv.Visit_Date__c;
                Integer Minute=0;
                DateTime visitDt;
                for(RoutingPlan__c r:rp)
                {
                    visitDt=DateTime.newInstance(dt.year(),dt.month(),dt.day(),8,Minute,1);
                    r.StartDateTime__c=visitDt;
                    r.EndDateTime__c=visitDt.addMinutes(30);
                    r.PlanStatus__c='waiting';
                    Minute+=31;
                }    
                try
                {        
                    update rp;     
                }  
                catch(DmlException ex)
                {
                    
                    rpv.addError('不能修改当前的记录');
                }     
            }*/
        }
    }
    else
    {
        List<Event> eList=new List<Event>();
        List<RoutingPlan__c> lp=[select Id from RoutingPlan__c where RoutingPlanAndVisit__c in:listO];
        delete lp;
        
        /* Date startTimeTem;
         Datetime startTime, endTime;            
         startTimeTem = trigger.old[0].Visit_Date__c;            
         startTime=DateTime.newInstance(startTimeTem.Year(),startTimeTem .Month(),startTimeTem.Day(),7,0,1);            
         endTime = startTime.addhours(16);
           
         Id ownerId = trigger.old[0].CreatedById;           
         for(Integer i=0;i<trigger.old.size();i++)           
         {                                        
             List<Event> eventsToDelete = [select Id,Subject,StartDateTime,EndDateTime from Event where StartDateTime >:startTime and EndDateTime <:endTime  and OwnerId = :ownerId];                
             if (eventsToDelete <> null)                 
             {                    
                 for(Event e:eventsToDelete)                 
                     eList.add(e);                 
             }           
         }
         delete eList;*/
    }
}